package com.qfedu.web;

import com.qfedu.pojo.*;
import com.qfedu.service.AccountService;
import com.qfedu.service.CardsService;
import com.qfedu.service.InforService;
import com.qfedu.service.User_cardsService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.CardsServiceImpl;
import com.qfedu.service.impl.InforServiceImpl;
import com.qfedu.service.impl.User_cardsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/accountServlet")
public class AccountServlet extends BaseServlet {

    private InforService inforService = new InforServiceImpl();
    private AccountService accountService = new AccountServiceImpl();
    private User_cardsService user_cardsService = new User_cardsServiceImpl();
    private CardsService cardsService = new CardsServiceImpl();
    //方法名必须和对应的action隐藏域的值保持一致


    public void openAccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、获取请求的参数
        String phone = request.getParameter("phone");
        String idcode = request.getParameter("idcode");
        String name = request.getParameter("name");
        String paypassword = request.getParameter("paypassword");
        //2、检查是否实名认证
        Infor infor = new Infor(phone, name, idcode);
        Account account = new Account(phone, idcode, name, paypassword, new BigDecimal(0));
        if (inforService.existsInfor(infor)) {
            //3、检查电话是否可用
            if (accountService.existsPhone(phone)) {
                System.out.println("电话[" + phone + "]已存在!");
                //跳回开户页面
                request.setAttribute("msg","该号码已开户");
                request.setAttribute("name",name);
                request.getRequestDispatcher("/pages/user/openaccount.jsp").forward(request, response);
            } else {
                //4、检查身份证是否可用
                if (accountService.existsIdcode(idcode)) {
                    System.out.println("身份证[" + idcode + "]已存在!");
                    request.setAttribute("msg","该身份证已开户");
                    request.setAttribute("name",name);
                    //跳回开户页面
                    request.getRequestDispatcher("/pages/user/openaccount.jsp").forward(request, response);
                } else {
                    //可用
                    //调用Service保存到数据库
                    accountService.openAccount(account);
                    //跳到开户成功页面 regist_success.html
                    request.getRequestDispatcher("/pages/user/openaccount_success.jsp").forward(request, response);

                }

            }
        }else{
            System.out.println("实名认证失败");
            request.setAttribute("msg","实名信息有误!");
            request.setAttribute("name",name);
            request.getRequestDispatcher("/pages/user/openaccount.jsp").forward(request, response);
        }

    }

    public void tiedCard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、获取请求的参数
        String name = request.getParameter("name");
        String idcode = request.getParameter("idcode");
        String cardnumber = request.getParameter("cardnumber");
        String bank_name = request.getParameter("bank_name");
        String phone = request.getParameter("phone");
        String paypassword = request.getParameter("paypassword");
        String code = request.getParameter("code");
        Account account = accountService.queryAccountByIdcode(idcode);
        User_cards user_cards = user_cardsService.queryUser_cards(cardnumber,name,phone);
        Cards cards = new Cards(idcode,name,cardnumber,user_cards.getMoney(),bank_name);
        String attr = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //2、检查验证码是否输入正确
        if(attr.equalsIgnoreCase(code)){
            //3、检查银行卡信息是否匹配
            if (user_cardsService.existsUser_cards(cardnumber,name,phone)) {
                //4、检查银行开卡手机号是否与开户手机一致(即所填手机号是否开户)
                if(accountService.existsPhone(phone)){
                    //5.检查输入的支付密码是否跟帐号支付密码一致
                    if(paypassword.equals(account.getPaypassword())){
                        //开卡。存进数据库
                        cardsService.tiedCard(cards);
                        //跳到开卡成功页面 tiedcard_success.jsp
                        request.getRequestDispatcher("/pages/user/tiedcard_success.jsp").forward(request, response);

                    }else {
                        request.setAttribute("msg", "输入密码与帐号支付密码不一致");
                        request.setAttribute("name", name);
                        request.setAttribute("idcode", idcode);
                        request.setAttribute("cardnumber", cardnumber);
                        request.setAttribute("bank_name", bank_name);
                        request.getRequestDispatcher("/pages/user/tiedcard.jsp").forward(request, response);
                    }

                }else {
                    request.setAttribute("msg", "银行开卡手机与开户手机号不一致！");
                    request.setAttribute("name", name);
                    request.setAttribute("idcode", idcode);
                    request.setAttribute("cardnumber", cardnumber);
                    request.setAttribute("bank_name", bank_name);
                    request.getRequestDispatcher("/pages/user/tiedcard.jsp").forward(request, response);
                }


            }else {
                request.setAttribute("msg", "银行卡信息有误！");
                request.getRequestDispatcher("/pages/user/tiedcard.jsp").forward(request, response);
            }

        }else{
            request.setAttribute("msg", "验证码错误！");
            request.setAttribute("name", name);
            request.setAttribute("idcode", idcode);
            request.setAttribute("cardnumber", cardnumber);
            request.setAttribute("bank_name", bank_name);
            request.setAttribute("phone", phone);
            request.getRequestDispatcher("/pages/user/tiedcard.jsp").forward(request, response);
        }


    }

    public void isOpen(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if (user!=null){
            Account account = accountService.queryIdcodeByPhone(user.getPhone());
            if (account!=null){
                response.sendRedirect("/page/isOpen.jsp");
            }else {
                response.sendRedirect("/pages/user/openaccount.jsp");
            }
        }else {
            response.sendRedirect("/pages/user/login.jsp");
        }
    }
}